package com.ld.controller;

import com.ld.annotation.LdPermission;
import com.ld.doamin.Department;
import com.ld.doamin.PageListQuery;
import com.ld.service.IDepartmentService;
import com.ld.basic.utils.AjaxResult;
import com.ld.basic.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 为了让自定义注解在一开始就进行加载生效，所以使用springmvc中的监听器ServletContextListener
 */
@LdPermission(name="部门管理",desc = "部门的操作") //自定义注解
@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;


    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除方法" )
    @LdPermission(name="按id删除",desc = "按照所给的id值删除该条数据")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询方法" )
    @LdPermission(name = "按id查询",desc = "根据id值查询")
    public AjaxResult getById(@PathVariable("id")Long id){
        try {
            Department department = service.loadById(id);
            return AjaxResult.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有" )
    @LdPermission(name = "查询所有",desc ="仅做到查询所有")
    public AjaxResult getAll(){
        try {
            List<Department> departments = service.loadAll();
            return AjaxResult.me().setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }
    }

    /**
     * 增加/修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "增加或者修改，用id区分" )
    @LdPermission(name = "保存页面",desc = "增加和修改都用这个")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            System.out.println(department);
            Long id = department.getId();
            if (id!=null){
                service.update(department);
                return AjaxResult.me();
            }else {
                service.insert(department);
                return AjaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    @LdPermission(name = "分页查询",desc = "分页使用该查询方法")
    public AjaxResult queryPage(@RequestBody PageListQuery query){
        try {
            PageList<Department> result= service.queryPage(query);
      return AjaxResult.me().setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("分页查询出错");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @LdPermission(name = "批量删除",desc = "删除多条数据")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
           return AjaxResult.me().setSuccess(false).setMsg("删除失败："+e.getMessage());
        }
    }

    @GetMapping("/tree")
    @LdPermission(name = "联级查询",desc = "用于连级展示页面")
    public AjaxResult getTree(){
        try {
            List<Department> tree= service.getTree();
            return AjaxResult.me().setData(tree);
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMsg("查询失败："+e.getMessage());
        }
    }
}
